package cn.eonshine.cuba.inf.modular.security.controller;

import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.eonshine.base.core.base.controller.BaseController;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.common.http.Message;
import cn.eonshine.cuba.inf.modular.security.server.IKeyService;

/**
 * 服务端安全公钥获取
 * @author lanjf2000
 *
 */
@RestController
@RequestMapping("/securityKey")
public class KeyController extends BaseController{


	@Autowired
	private IKeyService keyService;
	
	@RequestMapping("/getPublicKeyBase64")
	public Message getPublicKeyBase64() {
		Message message = new Message();
		
		int status = InfStatus.INF_OK;
		String msg = null;
		int count = 0;
		try {
			String pubKey = keyService.getLocalPubKeyBase64();
			message.setPubKey(pubKey);
		} catch (UnrecoverableKeyException e) {
			status = InfStatus.INF_ERROR;
			msg = Message.getErrorMsg(e);
			e.printStackTrace();
		} catch (KeyStoreException e) {
			status = InfStatus.INF_ERROR;
			msg = Message.getErrorMsg(e);
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			status = InfStatus.INF_ERROR;
			msg = Message.getErrorMsg(e);
			e.printStackTrace();
		} catch (CertificateException e) {
			status = InfStatus.INF_ERROR;
			msg = Message.getErrorMsg(e);
			e.printStackTrace();
		} catch (IOException e) {
			status = InfStatus.INF_ERROR;
			msg = Message.getErrorMsg(e);
			e.printStackTrace();
		}
		

		message.setStatus(status);
		message.setMsg(msg);
		message.setCount(count);
		
		return message;
	}
}
